/*
 * @FilePath: \undefinedc:\Users\sxjct\.leetcode\357.计算各个位数不同的数字个数.cpp
 * @Brief: 
 * @Version: 1.0
 * @Date: 2021-03-23 10:44:06
 * @Author: tianyiyi
 * @Copyright: Copyright@tianyiyi
 * @LastEditors: Mr.Tian
 * @LastEditTime: 2021-03-23 11:15:03
 */
/*
 * @lc app=leetcode.cn id=357 lang=cpp
 *
 * [357] 计算各个位数不同的数字个数
 */

// @lc code=start

/* 超时
class Solution {
    int res;
public:
    int countNumbersWithUniqueDigits(int n) {
        vector<int>path;
        backtrack(path,n);
        return res;
    }

    void backtrack(vector<int> &path,int n)
    {
        if(path.size() == n)
        {
            res++;
        }

        for(int i = 0;i<10;i++)
        {
            if(path.size() == 0 && i==0)
            {
                continue;
            }
            if(path[0] == i)
            {
                continue;
            }
            path.push_back(i);
            backtrack(path,n);
            path.pop_back();
        }


    }
};

*/

class Solution {
    int res;
public:
    int countNumbersWithUniqueDigits(int n) {
        if(n == 0)
        {return 1;}
        if(n == 1)
        {
            return 10;
        }
        vector<int>dp(n+1,0);
        dp[0] = 1;
        dp[1] = 9;
        int sum = dp[0] + dp[1];
        for(int i = 2; i <= n;i++)
        {
            dp[i] = dp[i - 1] * (10 - (i - 1)); 
            sum+=dp[i];
        }

        return sum;
    }

};

// @lc code=end

